package com.googlecode.hessianserver.example;

import java.math.BigInteger;

import com.caucho.hessian.client.HessianProxyFactory;
import com.googlecode.hessianserver.example.api.Calculator;

/**
 * Starts up example hessian proxy services and calls their remote methods.
 * <p>
 * This code is not dependant on HessianServer in any way, i.e. no changes
 * necessary to run against a regular hessian service implementation that
 * extended HessianServlet running inside a servlet container.
 * <p>
 * The ExampleServer should be running before invoking the client
 *
 * @author rayvanderborght
 */
public class ExampleClient
{
	/**
	 * Demonstrates client side calls to HessianServer.
	 *
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception
	{
		String url = "http://localhost:9090/calculatorService";

		HessianProxyFactory factory = new HessianProxyFactory();
		factory.setOverloadEnabled(true);
		Calculator calculator = (Calculator)factory.create(Calculator.class, url);

		System.out.println("add(1, 1): "		+ calculator.add(1, 1));
		System.out.println("multiply(2, 3): "	+ calculator.multiply(2, 3));
		System.out.println("divide(5, 2): "		+ calculator.divide(5, 2));

		try
		{
			System.out.println("divide(1, 0): " + calculator.divide(1, 0));
		}
		catch (ArithmeticException e)
		{
			System.out.println("divide(1, 0): Caught / by zero for call to ");
		}

		System.out.println("add(BigInteger.valueOf(Long.MAX_VALUE), BigInteger.valueOf(Long.MAX_VALUE)): " + calculator.add(BigInteger.valueOf(Long.MAX_VALUE), BigInteger.valueOf(Long.MAX_VALUE)));
	}
}
